package cn.edu.buu.news.controller;

import cn.edu.buu.news.entity.LearningPath;
import cn.edu.buu.news.service.LearningPathService;
import cn.edu.buu.news.vo.PageQuery;
import cn.edu.buu.news.vo.PageResult;
import cn.edu.buu.news.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/learningPath")
public class LearningPathController {

    @Autowired
    private LearningPathService learningPathService;

    // 新增学习路径
    @PostMapping
    public Result add(@RequestBody LearningPath learningPath) {
        learningPathService.add(learningPath);
        return Result.success();
    }

    // 删除学习路径
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        learningPathService.deleteById(id);
        return Result.success();
    }

    // 更新学习路径
    @PutMapping
    public Result update(@RequestBody LearningPath learningPath) {
        learningPathService.update(learningPath);
        return Result.success();
    }

    // 根据ID查询
    @GetMapping("/{id}")
    public Result<LearningPath> getById(@PathVariable Long id) {
        return Result.success(learningPathService.findById(id));
    }

    // 查询所有
    @GetMapping
    public Result<List<LearningPath>> getAll() {
        return Result.success(learningPathService.findAll());
    }

    // 分页查询
    @PostMapping("/list")
    public Result<PageResult<LearningPath>> getByPage(@RequestBody PageQuery query) {
        return Result.success(learningPathService.findByPage(query));
    }
}